﻿using BoardBroker;
using Nini.Config;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace BoardBroker
{
    public class ConfigService
    {
        #region static fields
        /// <summary>
        /// 配置文件名称
        /// </summary>
        private const string ConfigFileName = "DeviceControl.ini";

        public static void SaveConfig<T>(T value, string section) where T : class, new()
        {
            Type type = value.GetType();
            IniConfigSource source = new IniConfigSource($".\\{ConfigFileName}");
            IConfig config = source.Configs[section];
            PropertyInfo[] pArray = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
            foreach (PropertyInfo pi in pArray)
            {
                string field = pi.Name;
                object val = pi.GetValue(value, null);
                if (val == null)
                {
                    continue;
                }
                object valOld = config.Get(field);
                if (!val.ToString().Equals(valOld.ToString()))
                {
                    config.Set(field, val);
                }
            }
            source.Save();
        }

        public static T LoadConfig<T>(string section) where T : class, new()
        {
            Type type = typeof(T);
            T value = new T();
            IniConfigSource source = new IniConfigSource(ConfigFileName);
            IConfig config = source.Configs[section];
            PropertyInfo[] pArray = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
            foreach (PropertyInfo pi in pArray)
            {
                string field = pi.Name;
                object val = config.Get(field);
                if (val == null)
                {
                    continue;
                }
                pi.SetValue(value, val);
            }
            return value;
        }
        #endregion

        private static DeviceControlWindowInfo _led = null;
        public static DeviceControlWindowInfo Led
        {
            get
            {
                if(_led != null)
                {
                    return _led;
                }
                _led = LoadConfig<DeviceControlWindowInfo>("Led");
                return _led;
            }
        }

        private static LedNumAdaptDeviceSetting _ledAdapt = null;
        public static LedNumAdaptDeviceSetting LedAdapt
        {
            get
            {
                if (_ledAdapt != null)
                {
                    return _ledAdapt;
                }
                _ledAdapt = LoadConfig<LedNumAdaptDeviceSetting>("LedAdapt");
                return _ledAdapt;
            }
        }

        private static DeviceControlWindowInfo _usbConfig = null;
        public static DeviceControlWindowInfo USB
        {
            get
            {
                if (_usbConfig != null)
                {
                    return _usbConfig;
                }
                _usbConfig = LoadConfig<DeviceControlWindowInfo>("USB");
                return _usbConfig;
            }
        }
    }
}
